package org.common.utils.fury;
import io.fury.Fury;
import org.common.utils.thread.entry.ThreadEntryPool;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * Fury对象池
 * @author zhouzhibing
 * @date 2025/11/21 17:38
 */
public class FuryPool {

    private static final Logger log = LoggerFactory.getLogger(FuryPool.class);

    /**
     * 线程池中Fury对象超时时间
     */
    private static final int TIMEOUT = 5000;
    /**
     * Fury对象池
     */
    private final ThreadEntryPool<Fury> threadEntryPool = new ThreadEntryPool<>(TIMEOUT , FuryPool::newFury);

    /**
     * 获取Fury实例
     * @return Fury实例
     */
    public Fury get() {
        return threadEntryPool.getEntry();
    }

    /**
     * 归还Fury实例
     */
    public void returnEntry() {
        threadEntryPool.returnEntry();
    }

    /**
     * 创建Fury实例
     * @return Fury实例
     */
    private static Fury newFury() {
        return Fury.builder()
                .withLanguage(io.fury.config.Language.JAVA)
                .withAsyncCompilation(true)            // 异步编译序列化器，提升启动性能
                .withCodegen(true)                     // 启用代码生成，大幅提升性能
                .withRefTracking(true)
                .withStringCompressed(true)             //字符串压缩
                .requireClassRegistration(false)        //关闭强制类注册
                .build();
    }
}
